package com.j180.spring_data_jpa_2demo.service.impl;

import com.j180.spring_data_jpa_2demo.bean.one2many.Customer;
import com.j180.spring_data_jpa_2demo.bean.one2many.Order;
import com.j180.spring_data_jpa_2demo.dao.ICustomerDao;
import com.j180.spring_data_jpa_2demo.dao.IOrderDao;
import com.j180.spring_data_jpa_2demo.service.IOrderService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements IOrderService {
    @Resource
    private IOrderDao iOrderDao;
    @Override
    public Page<Order> getOrdersByCondition(Map<String, String> condition) {
        Page<Order> page = null;
        //获得客户端的条件
        //当前页数
        String curpage = condition.get("curpage");
        //每页显示条数
        String pageSize = condition.get("pageSize");
        //用户名的名字
        String customerName = condition.get("customerName");
        //用户的性别
        String gender = condition.get("customerGender");
        //订单的的名称
        String orderName = condition.get("orderName");
        //订单的的下单时间
        String orderTime = condition.get("orderTime");
        //创建分页对象
        PageRequest pageRequest =PageRequest.of(Integer.parseInt(curpage)-1,Integer.parseInt(pageSize), Sort.by("id").descending());

        //Specification 查询的条件 (你可以认为predicate 就是拼接到where的条件)
      page = iOrderDao.findAll(new Specification<Order>() {
            @Override
            public Predicate toPredicate(Root<Order> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

                Predicate predicate =null;
                //装添加的集合
                List<Predicate> list = new ArrayList<>();
                //封装条件
                if(!"".equals(customerName)&&customerName!=null){
                        //root.get("customer") 获得需要order对象中的属性名
                        //      name是Customer对象的name属性
                        //如果需要使用关联对象就继续点
                        list.add(criteriaBuilder.like(root.get("customer").get("name").as(String.class),"%"+customerName+"%"));
                    }
                    if(!"".equals(gender)&&gender!=null){

                        list.add(criteriaBuilder.equal(root.get("customer").get("gender").as(String.class),gender));
                    }
                    if(!"".equals(orderName)&&orderName!=null){
                        list.add(criteriaBuilder.like(root.get("goodName").as(String.class),"%"+orderName+"%"));
                    };

                if(!"".equals(orderTime)&&orderTime!=null){
                    list.add(criteriaBuilder.greaterThanOrEqualTo(root.get("localDateTime").as(String.class),orderTime));
                };

                //把多个条件转为一个
                predicate  =criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
                return predicate;
            }
        },pageRequest);


        return page;
    }
}
